<?php
defined('IN_TS') or die('Access Denied.');
switch($ts){

	//确认收货
	case "receive":

		$js = intval($_GET['js']);

    	$userid = intval($TS_USER['userid']);

		if($userid == 0) {
			getJson('请登录后再操作！',1);
		}

		$giftid = intval($_POST['giftid']);
		$strGift = $new['gift']->find('gift',array(
			'giftid'=>$giftid,
		));


		$userNum = $new['gift']->findCount('gift_users',array(
			'giftid'=>$giftid,
			'userid'=>$userid,
		));

		if($userNum==0){
			getJson('您没有下过单',1);
		}


		//标记已收货
		$new['gift']->update('gift_users',array(
			'giftid'=>$giftid,
			'userid'=>$userid,
		),array(
			'status'=>3,
		));


		if($strGift['cate']==0){
			// 对积分进行处理
			aac('user') -> doScore($TS_URL['app'], $TS_URL['ac'], $TS_URL['ts'],$userid,-$strGift['price']);
		}

		//统计领取的
		$sql = "select sum(quota) as sums from ts_gift_users where giftid=".$giftid.' and status=3';
		$arr=$db->once_fetch_assoc($sql);
		$userDoNum=$arr['sums'];

		$arr1['count_userdo']=$userDoNum;
		if($userDoNum>=$strGift['quota']){
			$arr1['status']=1;
		}

		$new['gift']->update('gift',array(
			'giftid'=>$giftid,
		),$arr1);

		//发一条广播
		$content = '收到了<a href="'.tsUrl('user','duty',array('id'=>$strGift['userid'])).'">'.$arrUser['username'].'</a>义卖的<a href="'.tsUrl('gift','show',array('id'=>$giftid)).'">'.$strGift['title'].'！</a>';
		aac('weibo')->create('weibo',array(
			'userid'=>$TS_USER['userid'],
			'content'=>$content,
			'isaudit'=>0,
			'addtime'=>date('Y-m-d H:i:s'),
			'uptime'=>date('Y-m-d H:i:s'),
		));
		//发送消息给卖家
		$msg_userid = '0';
		$msg_touserid = $strGift['userid'];
		$msg_content = '你捐赠的《'.$strGift['title'].'》，买家已收货，义卖证书已经生成，请点击后面的链接访问，可以下载图片转发到你的朋友圈。';
		$msg_tourl = tsUrl('gift','cert',array('id'=>$giftid));
		aac('message')->sendmsg($msg_userid,$msg_touserid,$msg_content,$msg_tourl);


		echo '1';

		break;

	//确认没收货
	case "noreceive":

		$js = intval($_GET['js']);
		$userid = intval($TS_USER['userid']);

		if($userid == 0) {
			getJson('请登录后再操作！',1);
		}

		$giftid = intval($_POST['giftid']);
		$strGift = $new['gift']->find('gift',array(
			'giftid'=>$giftid,
		));


		$userNum = $new['gift']->findCount('gift_users',array(
			'giftid'=>$giftid,
			'userid'=>$userid,
		));

		if($userNum==0){
			getJson('您没有下过单',1);
		}


		//标记没收货
		$new['gift']->update('gift_users',array(
			'giftid'=>$giftid,
			'userid'=>$userid,
		),array(
			'status'=>2,
		));

		if($strGift['cate']==0) {
			aac('user')->doScore($TS_URL['app'], $TS_URL['ac'], $TS_URL['ts'], $userid, $strGift['price']);
		}


		//统计领取的
		$sql = "select sum(quota) as sums from ts_gift_users where giftid=".$giftid.' and status=3';
		$arr=$db->once_fetch_assoc($sql);
		$userDoNum=$arr['sums'];

		$new['gift']->update('gift',array(
			'giftid'=>$giftid,
		),array(
			'count_userdo'=>$userDoNum
		));


		echo '1';

		break;

	//确认发货
	case "send":

		$js = intval($_GET['js']);

		$userid = intval($TS_USER['userid']);

		if($userid == 0) {
			getJson('请登录后再操作！',1);
		}

		$giftid = intval($_POST['giftid']);
		$memberid = intval($_POST['memberid']);
		$strGift = $new['gift']->find('gift',array(
			'giftid'=>$giftid,
		));


		$userNum = $new['gift']->findCount('gift_users',array(
			'giftid'=>$giftid,
			'userid'=>$memberid,
		));

		if($userNum==0){
			getJson('此人没有下过单',1);
		}


		//标记已发货
		$new['gift']->update('gift_users',array(
			'giftid'=>$giftid,
			'userid'=>$memberid,
		),array(
			'status'=>2,
		));
		//发一条便笺
		$content = '义卖的<a href="'.tsUrl('gift','show',array('id'=>$giftid)).'">'.$strGift['title'].'发货了！</a>';
		aac('weibo')->create('weibo',array(
			'userid'=>$TS_USER['userid'],
			'content'=>$content,
			'isaudit'=>0,
			'addtime'=>date('Y-m-d H:i:s'),
			'uptime'=>date('Y-m-d H:i:s'),
		));

		//发送消息给买家
		$msg_userid = $userid;
		$msg_touserid = $memberid;
		$msg_content = '《'.$strGift['title'].'》已发货，请注意查收。收货后请访问链接，点击确认收货。';
		$msg_tourl = tsUrl('gift','show',array('id'=>$giftid));
		aac('message')->sendmsg($msg_userid,$msg_touserid,$msg_content,$msg_tourl);




		echo '1';

		break;

	//确认没发货
	case "nosend":

		$js = intval($_GET['js']);
		$userid = intval($TS_USER['userid']);

		if($userid == 0) {
			getJson('请登录后再操作！',1);
		}

		$giftid = intval($_POST['giftid']);
		$memberid = intval($_POST['memberid']);
		$strGift = $new['gift']->find('gift',array(
			'giftid'=>$giftid,
		));


		$userNum = $new['gift']->findCount('gift_users',array(
			'giftid'=>$giftid,
			'userid'=>$memberid,
		));

		if($userNum==0){
			getJson('此人没有下过单',1);
		}



		//标记没发货
		$new['gift']->update('gift_users',array(
			'giftid'=>$giftid,
			'userid'=>$memberid,
		),array(
			'status'=>1,
		));


		echo '1';

		break;

	//取消申领商品
	case "cancel":

		$js = intval($_GET['js']);

		$userid = intval($TS_USER['userid']);
		if($userid == 0) {
			getJson('请登录后操作',$js);
		}
		$giftid = intval($_POST['giftid']);
		$arrGift = aac('gift')->find('gift',array('giftid'=>$giftid));

		if($arrGift['userid']!=$userid&&$TS_USER['isadmin']==''){
			getJson('没有操作权限',$js);
		}


		
		$new['gift']->delete('gift_users',array(
		
			'giftid'=>$giftid,
			'userid'=>$userid,
		
		));
		
		//统计一下申领的
		$userGiftNum = $new['gift']->findCount('gift_users',array(
			'giftid'=>$giftid,
			'status'=>0,
		));
		


		$new['gift']->update('gift',array(
			'giftid'=>$giftid,
		),array(
			'count_userwish'=>$userGiftNum,
		));
		
		echo '1';
		
		break;
	//退款
	case "refund":

		$js = intval($_GET['js']);

		$userid = intval($TS_USER['userid']);
		if($userid == 0) {
			getJson('请登录后操作',$js);
		}
		if($TS_USER['isadmin']==''){
			getJson('没有操作权限',$js);
		}

		$payid = intval($_POST['payid']);

		$arrPay = aac('pay')->find('pay_user',array('payid'=>$payid));
		aac('pay')->update('pay_user',array('payid'=>$payid),array('status'=>-1));
		$arrGift = aac('gift')->find('gift_users',array(
			'orderid'=>$arrPay['orderid']));
		aac('gift')->update('gift_users',array(
			'orderid'=>$arrPay['orderid']),array(
				'status'=>-1
			)
		);

		//统计一下申领的
		$userGiftNum = $new['gift']->findCount('gift_users',array(
			'giftid'=>$arrGift['giftid'],
			'status'=>0,
		));



		$new['gift']->update('gift',array(
			'giftid'=>$arrGift['giftid'],
		),array(
			'count_userwish'=>$userGiftNum,
		));

		echo '1';

		break;

	//上架商品
	case "upsell":
		$js = intval($_GET['js']);

		$userid = intval($TS_USER['userid']);
		if($userid == 0) {
			getJson('请登录后操作',$js);
		}
		$giftid = intval($_POST['giftid']);
		$arrGift = aac('gift')->find('gift',array('giftid'=>$giftid));

		if($arrGift['userid']!=$userid&&$TS_USER['isadmin']==''){
			getJson('没有操作权限',$js);
		}

		$new['gift']->update('gift',array(
			'giftid'=>$giftid,
		),array(
			'status'=>0,
			'limittime'=>strtotime('3 day'),
		));


		echo '1';

		break;
	//下架商品
	case "downsell":
		$js = intval($_GET['js']);

		$userid = intval($TS_USER['userid']);
		if($userid == 0) {
			getJson('请登录后操作',$js);
		}
		$giftid = intval($_POST['giftid']);
		$arrGift = aac('gift')->find('gift',array('giftid'=>$giftid));

		if($arrGift['userid']!=$userid&&$TS_USER['isadmin']==''){
			getJson('没有操作权限',$js);
		}

		$new['gift']->update('gift',array(
			'giftid'=>$giftid,
		),array(
			'status'=>2,
		));


		echo '1';

		break;

	//申领商品
	case "join":
	
		$js = intval($_GET['js']);
		
	
		$userid = intval($TS_USER['userid']);
		
		if($userid == 0) {
			getJson('请登录后操作',$js);
		}

		$strUser = aac('user')->find('user_info',array('userid'=>$userid));
		
		$giftid = intval($_POST['giftid']);

        $strGift = $new['gift']->find('gift',array(
            'giftid'=>$giftid,
        ));

		$arrGiftUser = $new['gift']->find('gift_users',array(
			'giftid'=>$giftid,
			'userid'=>$userid,
		));

        if($strGift==''){
            getJson('不存在该商品！',$js);
        }
		if($arrGiftUser){
			getJson('请勿重复操作',$js);
		}
		if($strGift['limittime']<=time()){
			getJson('已过有效期',$js);
		}


        if($strGift['cate']==0&&$strGift['price']>0){

			if($strUser['count_score']<$strGift['price']){
				getJson('您的积分不够申领福利',$js);
			}
        }


		$username = trim($_POST['username']);
		$phone = trim($_POST['phone']);
		$province = trim($_POST['province']);
		$city = trim($_POST['city']);
		$address = trim($_POST['address']);
		
		if($username=='' || $phone==''){
			getJson('姓名和手机号不能为空',$js);
		}


		if($strGift['transfer']==2&&($province==''||$city==''||$address=='')){
			getJson('请填写完整收货地址',$js);
		}



		if($strGift['cate']==0){
			$quota = 1;
			$money = 0;
		}else{
			$quota = trim($_POST['quota']);
			if($quota ==''){
				getJson('数量必须填',$js);
			}
			$money = $strGift['price']*$quota;
		}

		if($quota>$strGift['quota']-$strGift['count_userwish']){
			getJson('您下单的数量大于当前的库存',$js);
		}


		$arr = array(
			'giftid'=>$giftid,
			'userid'=>$userid,
			'username'=>$username,
			'phone'=>$phone,
			'quota' => $quota,
			'money' => $money,
			'transfer'=>$strGift['transfer'],
			'province'=> $province,
			'city'  => $city,
			'address'=> $address,
			'addtime'=>time(),
		);
		if($strGift['cate']==0){
			$arr['status']=1;
		}else{
			$arr['status']=0;
		}

		$new['gift']->create('gift_users',$arr);

		/*更新用户信息
		$new['gift']-> update('user_info',array(
			'userid'=>$userid,
		),array(
			'username'=>$username,
			'phone'=>$phone,
		));*/


		//统计一下申领的
		$sql = "select sum(quota) as sums from ts_gift_users where giftid=".$giftid.' and status>-1';
		$arr=$db->once_fetch_assoc($sql);
		$userWishNum=$arr['sums'];

		$arr1['count_userwish']=$userWishNum;
		if($userDoNum>=$strGift['quota']){
			$arr1['status']=1;
		}
		$new['gift']->update('gift',array(
			'giftid'=>$giftid,
		),$arr1);

		//发一条便笺
		$content = '下单了'.'<a href="'.tsUrl('user','duty',array('id'=>$strGift['userid'])).'">'.$arrUser['username'].'</a>'.'义卖的<a href="'.tsUrl('gift','show',array('id'=>$giftid)).'">'.$strGift['title'].'！</a>';
		aac('weibo')->create('weibo',array(
			'userid'=>$TS_USER['userid'],
			'content'=>$content,
			'isaudit'=>0,
			'addtime'=>date('Y-m-d H:i:s'),
			'uptime'=>date('Y-m-d H:i:s'),
		));

		if($strGift['userid'] != $userid){
			$msg_userid = '0';
			$msg_touserid = $strGift['userid'];
			$msg_content = '你发布的商品《'.$strGift['title'].'》有人下单了，快去看看吧^_^ ';
			$msg_tourl = tsUrl('gift','show',array('id'=>$giftid));
			aac('message')->sendmsg($msg_userid,$msg_touserid,$msg_content,$msg_tourl);
		}


		header('location: '.$_SERVER['HTTP_REFERER']);


		getJson('下单成功，请等候我们与你联系。',1,1);
		
		
		break;

	//定期下架过期商品
	case 'updatestatus':
		$arrGift=aac('gift')->findAll('gift','status=0 and limittime<'.time());
		foreach($arrGift as $item){
			$new['gift']->update('gift',array(
				'giftid'=>$item['giftid'],
			),array(
				'status'=>-1
			));
			if($item['cate']==0){
				$catename='志愿者福利';
			}else{
				$catename='义卖品';
			}
			//发私信通知
			$userid = '0';
			$touserid = $item['userid'];
			$content = '您发布的'.$catename.'超过5天无人问津，已经下架，仅您本人可见。您可以通过编辑商品信息，延长有效期恢复上架。建议您加强推广力度，以便早日售出。';
			$url = tsUrl('gift','show',array('id'=>$arrGift['giftid']));
			aac('message')->sendmsg($userid,$touserid,$content,$url);

		}
	    break;
	case 'export':

		$beginyear = trim($_POST['beginyear']);
		$beginmonth = trim($_POST['beginmonth']);
		$beginday = trim($_POST['beginday']);
		$endyear = trim($_POST['endyear']);
		$endmonth = trim($_POST['endmonth']);
		$endday = trim($_POST['endday']);

		if($beginyear&&$beginmonth&&$beginday){
			$beginday = $beginyear . "-" . $beginmonth . "-" . $beginday;

		}else{
			$beginday ='2017-10-7';
		}
		$begindate = $beginday.' 0:00';

		if($endyear&&$endmonth&&$endday){
			$endday = $endyear . "-" . $endmonth . "-" . $endday;
		}else{
			$endday = date('Y-m-d',time());
		}
		$enddate = $endday.' 24:00';

		$arr = 'addtime>='.strtotime($begindate).' and addtime<='.strtotime($enddate).' and trade_status="TRADE_SUCCESS"';
		//Excel表格式,这里简略写了8列
		$letter = array('A','B','C','D','E','F');
		//表头数组
		$tableheader = array('订单号','物品','金额','捐赠人','买家','支付日期');
//全部交易

		$arrTrade = $new['gift']->findAll('pay_user',$arr,'addtime desc');
		$data = array();
		foreach($arrTrade as $key=>$item){
			$strGiftUser = aac('gift')->find('gift_users',array('orderid'=>$item['orderid']));
			$gift = aac('gift')->find('gift',array('giftid'=>$strGiftUser['giftid']));
			$seller = aac('user')->find('user_info',array('userid'=>$gift['userid']));
			$buyer = aac('user')->find('user_info',array('userid'=>$item['userid']));
			$data[$key] = array($item['trade_no'],$gift['title'],$item['price'],$seller['username'],$buyer['username'],date('Y-m-d H:i:s',$item['addtime']));

		}
		$filename='义卖交易明细表（'.$beginday.'至'.$endday.'）.xls';
		create_xls($data,$letter,$tableheader,$filename);

		output_xls($data,$filename);
		getJson('操作成功',1,1);
	    break;

	// 上传封面
    case "photo":
		$photo_id = mt_rand(100000, 999999);
		$dir = "gift";
        $arrUpload = tsUpload($_FILES['file'], $photo_id, $dir, array('jpg','gif','png', 'jpeg'));

        if($arrUpload){
			// 删除缓存缩略图	
            tsDimg($arrUpload['url'], $dir,'48','48',$arrUpload['path']);
            tsDimg($arrUpload['url'], $dir,'16','16',$arrUpload['path']);
            tsDimg($arrUpload['url'], $dir,'32','32',$arrUpload['path']);
            tsDimg($arrUpload['url'], $dir,'24','24',$arrUpload['path']);
            tsDimg($arrUpload['url'], $div,'120','120',$arrUpload['path']);

			echo $arrUpload["url"];
        }else{
			echo 0;
        }

        break;

		
}
